$(document).ready(function() {
	$("#demo").validate({
		rules:{
			"users.userName": {
				required :true,
				stringCheck :true,
				byteRangeLength : [ 2, 60 ]
			},
			"users.password":{
				//required: true,
                minlength: 6
			},
			"repeatPassword":{
				//required: true,
                minlength: 6,
                equalTo: "#password"
			},
			"users.realName":{
				required: true,
	    		maxlength: 10
			},
			"users.telNum":{
				required: false,
				isPhone: true
			},
			"rolesId":{
				required: true
			},
			"users.level":{
				required: true
			}
			/*
			"users.departmentId":{
				required: true
			},
			"users.classId":{
				required: true
			}
			*/
		},
		/* 设置错误信息 */
		messages : {
			"repeatPassword":{
            	equalTo: "必须与密码一致"
			},
			"rolesId":{
				required: "至少选择一项"
			}
		}
		//errorPlacement: function(error, element) {   
//	        if (element.attr("name") == "rolesId" )  {
//	            error.appendTo("#errMsg"); 
//	        } 
//	        else {    
//	            error.insertAfter(element); 
//	        }
//		}
	});

});

//返回roles的json数据
function queryRolesList(rolesId){
  $.post(
		"../AjaxResultData/listAllRoles.action",
		function(data){
			$("#rolesId").empty();
			$("#rolesId").append("<option value=''>--未选择--</option>");
			var rolesData = eval("("+data.rolesData+")");  
			for(var i = 0;i<rolesData.length;i++){
				if(rolesId==rolesData[i].rolesId){
					$("#rolesId").append("<option value='"+rolesData[i].rolesId+"' selected='selected'>"+rolesData[i].roleName+"</option>");
				}else{
					$("#rolesId").append("<option value='"+rolesData[i].rolesId+"'>"+rolesData[i].roleName+"</option>");
				}
			}
		},
		"json"
	);
}

//根据DepartmentId返回class的json数据
function queryClassByDepartmentId(){
	var departmentId = document.getElementById("departmentId").value;
	$.post(
		"../AjaxResultData/listAllClassByDepartmentId.action",
		{"classStu.departmentId":departmentId},
		function(data){
			var classData = eval("("+data.classData+")");
			for(var i = 0; i<classData.length; i++){
				$("#classId").append("<option value='"+classData[i].classId+"'>"+classData[i].className+"</option>");
			}
		}
	);
}
	
//返回department的json数据
function queryDepartmentList(){
  $.post(
		"../AjaxResultData/listAllDepartment.action",
		function(data){
			var departmentData = eval("("+data.departmentData+")");  
			$("#departmentId").append("<option value=''>--未选择--</option>");
			for(var i = 0;i<departmentData.length;i++){
				$("#departmentId").append("<option value='"+departmentData[i].departmentId+"'>"+departmentData[i].departmentName+"</option>");
			}
		},
		"json"
	);
}

function createDepartmentChange(){
	$("#departmentId").change(function(){
		$("#classId").empty();
		queryClassByDepartmentId();
	});
}

//点击提交按钮时处理角色和班级
function dealSubmit(){
//点击提交按钮时将班级和角色合并成字符串提交
$("#submit").click(function(){
//$("#rolesId").change(function(){
	//处理角色
	if(document.getElementsByName("rolesId")!=null){
		var str = document.getElementById("rolesId");
		var collection = "";
		for (i = 0; i < str.options.length; i++) {
			if (str[i].selected == true) {
				collection += str[i].value + ",";
			}
		}
		if (collection != "") {
			collection = collection.substring(0, collection.lastIndexOf(','));
			document.getElementById("users.roleCollection").value = collection;
		}
	}
	
	//处理班级
	if(document.getElementById("classId")!=null){
		str = document.getElementById("classId");
		collection = "";
		for (i = 0; i < str.options.length; i++) {
			if (str.options[i].selected == true) {
				collection += str.options[i].value + ",";
			}
		}
		if (collection != "") {
			collection = collection.substring(0, collection.lastIndexOf(','));
			document.getElementById("users.classCollection").value = collection;
		}
	}
});
}

//验证用户名是否可用
function checkUserNameIsNotUsed(){
	$("#userName").blur(function(){
		var userName = document.getElementById("userName").value;
		if(userName!=""){
			$.post(
				"../AjaxResultData/checkUserNameIsNotUsed.action",
				{"users.userName":userName},
				function(data){
					var isNotUsedData = eval("("+data.isNotUsedData+")");
					if(isNotUsedData[0].isNotUsed=="true"){
						var userNameTemp = $("#userName").attr("value");
						$("#ajaxShowResult").html("名为-"+userNameTemp+"-的用户名已存在，不能重复添加！");
						$("#userName").attr("value","");
					}else if(isNotUsedData[0].isNotUsed=="false"){
						$("#ajaxShowResult").html("用户名可用！");
					}
				},
				"json"
			);
		}else{
			$("#ajaxShowResult").html("");
		}
	});
}

//变更级别时修改
function createLevelChange(){
$("#level").change(function(){
	$("#departmentId").empty();
	$("#classId").empty();
	$("#departmentId").removeClass("required");
	$("#classId").removeClass("required");
	if($("#level").attr("value")==1){
		$("#departmentIdLine").hide();
		$("#classIdLine").hide();
	}else if($("#level").attr("value")==2){
		$("#departmentIdLine").show();
		$("#classIdLine").hide();
		$("#departmentId").addClass("required");
		queryDepartmentList();
	}else if($("#level").attr("value")==3){
		//$("#departmentIdLine").show();
		//$("#classIdLine").show();
		//$("#departmentId").addClass("required");
		//$("#classId").addClass("required");	
		//queryDepartmentList();
		
		$("#departmentIdLine").hide();
		$("#classIdLine").hide();
	}
});
}

//根据用户等级判断下级显示项
function initForm(operType,departmentId,classCollection){
	if(operType=="insert"){
		$("#departmentIdLine").hide();
		$("#classIdLine").hide();
		$("#password").addClass("required");
		$("#repeatPassword").addClass("required");
	}else if(operType=="update"){
		//根据用户等级判断下级显示项
		$("#password").removeClass("required");
		$("#repeatPassword").removeClass("required");
		if($("#level").attr("value")==1){
			$("#departmentIdLine").hide();
			$("#classIdLine").hide();
		}else if($("#level").attr("value")==2){
			$("#departmentIdLine").show();
			$("#classIdLine").hide();
			queryDepartmentListSelected(departmentId);
		}else if($("#level").attr("value")==3){
			//$("#departmentIdLine").show();
			//$("#classIdLine").show();
			//queryDepartmentListSelected(departmentId);
			//initClassOptionList(departmentId,classCollection);
			
			$("#departmentIdLine").hide();
			$("#classIdLine").hide();
		}
	}

}

//返回department的json数据，带被选择数据
function queryDepartmentListSelected(departmentId){
	$.post(
		"../AjaxResultData/listAllDepartment.action",
		function(data){
			var departmentData = eval("("+data.departmentData+")");  
			$("#departmentId").append("<option value=''>--未选择--</option>");
			for(var i = 0;i<departmentData.length;i++){
				if(departmentId==departmentData[i].departmentId){
					$("#departmentId").append("<option value='"+departmentData[i].departmentId+"' selected='selected'>"+departmentData[i].departmentName+"</option>");
				}else{
					$("#departmentId").append("<option value='"+departmentData[i].departmentId+"'>"+departmentData[i].departmentName+"</option>");
				}
			}
		},
			"json"
	);
}

//初始化用户拥有的班级列表
function initClassOptionList(departmentId,classCollection){
	var array = classCollection.split(",");
	var flag = false;
	$.post(
		"../AjaxResultData/listAllClassByDepartmentId.action",
		{"classStu.departmentId":departmentId},
		function(data){
			var classData = eval("("+data.classData+")");
			for(var i = 0; i<classData.length; i++){
				flag = false;
				for(var j=0;j<array.length;j++){
					if(array[j]==classData[i].classId){
						$("#classId").append("<option value='"+classData[i].classId+"' selected='selected'>"+classData[i].className+"</option>");
						flag = true;
						break;
					}
				}
				if(flag==false){
					$("#classId").append("<option value='"+classData[i].classId+"'>"+classData[i].className+"</option>");
				}
			}
		}
	);
}